
package com.weiqiao.mall.service.impl;

import com.google.gson.Gson;
import com.mingsoft.util.StringUtil;
import com.mongodb.client.model.Filters;
import com.weiqiao.mall.domain.FreightEntity;
import com.weiqiao.mall.repository.IFreightRepository;
import com.weiqiao.mall.service.IFreightService;
import com.weiqiao.mall.service.vo.AddressVO;
import com.weiqiao.mall.service.vo.FreightVO;
import com.weiqiao.utils.DateUtils;
import com.weiqiao.utils.WeiXinUtils;
import org.bson.Document;
import org.bson.conversions.Bson;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;


/**
 * 微信小程序商城 配置 业务层实现类，继承BaseWeiXinBizImpl，实现IShopkeeperBiz
 *
 * @author tiyer
 */
@Service("FreightBizImpl")
public class FreightServiceImpl extends BaseWeiXinServiceImpl implements IFreightService {

    /**
     * 注入运费管理管理持久化层
     */
    @Autowired
    @Qualifier("FreightRepositoryImpl")
    private IFreightRepository ifreightDao;


    /**
     * 转换时间戳为时间字符串
     */
    public String formateTime(String dateStr) {
        try {
            if (StringUtil.isLong(dateStr)) {
                String date = WeiXinUtils.format(new Date(Long.parseLong(dateStr)), "yyyy-MM-dd HH:mm");
                dateStr = date;
            }
        } catch (Exception e) {

            e.printStackTrace();
        }
        return dateStr;
    }

    @Override
    public boolean updateFreightService(FreightEntity freight) {
        // template_id 模板id是否为空来判断是新建 还是更新
        if (StringUtil.isBlank(freight.getTemplate_id())) {
            // 模板编号
            freight.setTemplate_id(String.valueOf(System.currentTimeMillis()));
            // 创建时间
            freight.setCreat_time(String.valueOf(System.currentTimeMillis()));
            // 编辑时间
            freight.setEditor_time(String.valueOf(System.currentTimeMillis()));
            // 0为未删除状态
            freight.setIs_del(0);
            return ifreightDao.insertFreight(freight);
        } else {
            // 编辑时间
            freight.setEditor_time(String.valueOf(System.currentTimeMillis()));
            String json = new Gson().toJson(freight);
            Document doc = Document.parse(json);
            Document document = new Document();
            document.append("company_id", freight.getCompany_id());
            document.append("template_id", freight.getTemplate_id());
            return ifreightDao.updateFreight(document, doc);
        }
    }

    @Override
    public FreightEntity getFreightInf(Bson filter) {
        FreightEntity entity = new FreightEntity();
        String str = ifreightDao.getOne(filter);
        if (!StringUtil.isBlank(str)) {
            entity = new Gson().fromJson(str, FreightEntity.class);
        }
        return entity;
    }


    @Override
    public List<FreightEntity> findFreightByBson(int company_id, Bson bson) {
        Document sort = new Document();
        sort.append("template_id", -1);
        List<FreightEntity> freight = new ArrayList<>();
        Gson gson = new Gson();
        List<String> list = ifreightDao.getFreightByFilter(company_id, bson, sort);
        if (!list.isEmpty()) {
            for (String a : list) {
                FreightEntity s = gson.fromJson(a, FreightEntity.class);
                freight.add(s);
            }
        }
        return freight;
    }

    @Override
    public boolean delFreight(FreightVO freightVO) {
        boolean result = false;
        if (!StringUtil.isBlank(freightVO.getTemplate_id())) {
            Document document = new Document();
            document.append("template_id", freightVO.getTemplate_id());
            document.append("company_id", freightVO.getCompany_id());
            FreightEntity fre = getFreightInf(document);
            fre.setIs_del(1);
            result = updateFreightService(fre);
        }
        return result;
    }

    @Override
    public Map getFreightList(AddressVO address) {
        Map map = new HashMap<>();
        Document document = new Document();
        document.append("company_id", address.getCompany_id());
        document.append("is_del", 0);
        // 获取运费模板
        List<FreightEntity> freight = this.findFreightByBson(address.getCompany_id(), document);
        if (CollectionUtils.isEmpty(freight)) {
            map.put("freight", "");
        }
        map.put("freight", freight);
        return map;
    }
}
